package org.example.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.reggie.common.BaseContext;
import org.example.reggie.common.R;
import org.example.reggie.dto.OrdersDto;
import org.example.reggie.entity.OrderDetail;
import org.example.reggie.entity.Orders;
import lombok.extern.slf4j.Slf4j;
import org.example.reggie.service.OrderDetailService;
import org.example.reggie.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 点餐
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    /**
     * 支付
     * @param ids
     * @return
     */
    @PostMapping("/payment")
    public R<String> payment(long[] ids){
        orderService.payment(ids);
        return R.success("ok");
    }
    /**
     *
     * 订单显示
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize, HttpSession session){
        Page<Orders>ordersPage=new Page<>(page,pageSize);
        Page<OrdersDto> ordersDto=new Page<>();
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, session.getAttribute("user"));
        orderService.page(ordersPage, wrapper);
        //    对象拷贝
        BeanUtils.copyProperties(ordersPage,ordersDto,"records");
        List<Orders> ordersList=ordersPage.getRecords();
        List<OrdersDto>ordersDtoList=null;
        ordersDtoList =ordersList.stream().map((item)->{
            OrdersDto ordersDto1=new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto1);
            //    订单明细id
            long id = item.getId();
            LambdaQueryWrapper<OrderDetail>wrapperQueryWrapper = new LambdaQueryWrapper<>();
            wrapperQueryWrapper.eq(OrderDetail::getOrderId,id);
            List<OrderDetail> orderDetailList = orderDetailService.list(wrapperQueryWrapper);
            if (orderDetailList!=null) {
                ordersDto1.setOrderDetails(orderDetailList);
            }
            return ordersDto1;
        }).collect(Collectors.toList());
        ordersDto.setRecords(ordersDtoList);
        return R.success(ordersDto);
    }
    /**
     * 管理订单明细
     */
    @Transactional
    @GetMapping("/page")
    public R<Page<Orders>> page(int page,int pageSize, String number,int userSign){
        Page<Orders> page1=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper=new LambdaQueryWrapper<>();
        if (number != null) {
            wrapper.eq(Orders::getNumber,number);
        }
        if (userSign==1){
            //      获取当前用户id
            Long userid = BaseContext.getCurrentId();
            wrapper.eq(Orders::getUserId,userid);
        }
        orderService.page(page1,wrapper);
        return R.success(page1);
    }
    /**
     *管理订单派送完成情况
     */
    @PutMapping
    public R<Orders> updateStatus(@RequestBody Orders order){
    Orders orders=orderService.updateStatus(order);
        return R.success(orders);
    }



    @Transactional
    @GetMapping("/detail")
    public R<List<OrderDetail>> detail(long number){
        LambdaQueryWrapper<OrderDetail> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getOrderId,number);
        log.info(number+"");
        List<OrderDetail> list=orderDetailService.list(wrapper);
       // number+"+"+list.size()
        return R.success(list);
    }
}
